package com.baizhi.controller;


import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.CmfzUser;
import com.baizhi.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("selectAll")
    @ResponseBody
    public Map selectAll(String name,Integer page, Integer limit){
        Page<CmfzUser> cmfzUserPage = userService.selectAll(name,page, limit);
        Map map = new HashMap<>();
        map.put("code",0);
        map.put("msg",0);
        map.put("count",cmfzUserPage.getTotal());
        map.put("data",cmfzUserPage.getRecords());
        return map;
    }


    @RequestMapping("upload")
    @ResponseBody
    public Map upload(MultipartFile file) throws Exception{
        String  newName= UUID.randomUUID().toString()+file.getOriginalFilename();
        file.transferTo(new File("E:\\upload",newName));
        Map map = new HashMap();
        map.put("newName",newName);
        return map;
    }
    @RequestMapping("insert")
    @ResponseBody
    public  Map   insert(CmfzUser cmfzUser){
        Map map = new HashMap<>();
        try {
            userService.insert(cmfzUser);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

   @RequestMapping("delete")
    @ResponseBody
    public  Map   delete(Integer id){
        Map map = new HashMap<>();
        try {
            userService.delete(id);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("selectOne")
    @ResponseBody
    public  CmfzUser   selectOne(Integer id){
        return userService.showOne(id);
    }


    @RequestMapping("update")
    @ResponseBody
    public  Map  update(CmfzUser cmfzUser){
        Map map = new HashMap<>();
        try {
            userService.update(cmfzUser);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public  Map  deleteAll(List<CmfzUser> ids){
        Map map = new HashMap<>();
        try {
            userService.deleteAll(ids);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("countSex")
    @ResponseBody
    public List<Integer>  selectCountSex(){
        return userService.selectCountSex();
    }

    @RequestMapping("selectPro")
    @LogAnnotation(content="查询省份人数",type="查询")
    @ResponseBody
    public Map  selectPro(){
        Map map = new HashMap<>();
        List<Map> maps = userService.selectPro();
         List<Object> list1=new ArrayList<>();
         List<Object> list2=new ArrayList<>();
        for (Map map1 : maps) {
            Object  userProvince =map1.get("name");
            Object counts = map1.get("value");
            list1.add(userProvince);
            list2.add(counts);
        }
        map.put("name",list1);
        map.put("value",list2);
        return map;
    }

    @RequestMapping("getByDayCount")
    @ResponseBody
    public Map getByDayCount(){
        return userService.selectByDayCount();
    }

    /**
     *  优化
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("selectAllCountThread")
    @ResponseBody
    public Map selectAllCountThread() throws Exception {
        long start = System.currentTimeMillis();
        Map map = userService.selectByThread();
        long end = System.currentTimeMillis();
        System.out.println(end-start+"ms");
        return map;
    }
    /**
     * 无优化
     * @return
     */
    @RequestMapping("selectAllCount")
    @ResponseBody
    public Map selectAllCount() {
        long start = System.currentTimeMillis();
        Map map = userService.selectAllCount();
        long end = System.currentTimeMillis();
        System.out.println(end-start+"ms");
        return map;
    }

    @RequestMapping("getSexCount")
    @LogAnnotation(content="查询男女人数",type="查询")
    @ResponseBody
    public List<Map> getSexCount(){
        return userService.selectBySexCount();
    }

}
